import os


def collect_fuction(filepath):
    points = []
    with open(filepath, "r") as file:
        lines = file.readlines()
    for line in lines:
        x1, y1, x, y = line.strip().split(" ")
        points.append([float(x1), float(y1), float(x), float(y)])
    return points


def extract_x_y_data(points, x, y, flag=0):
    """

    :param points: all data
    :param x: extract standard
    :param y: extract standard
    :param flag: default 0, extract x列; 1, extract y行
    :return: extracted
    """
    extract_data = []
    for point in points:
        if flag == 0 and point[2] == x:
            extract_data.append(point)
        elif flag == 1 and point[3] == y:
            extract_data.append(point)
    return extract_data


def calc_loss(points, x, y, flag=0):
    """

    :param points: data
    :param flag: default
    :param x: standard data
    :param y: standard data
    :return:
    """
    loss_sum = 0
    if flag == 0:
        standard = x
        index = 0
    else:
        standard = y
        index = 1
    for point in points:
        loss = abs(point[index] - standard)
        loss_sum += loss
    average_loss = loss_sum/len(points)
    return average_loss


def calc_soft_loss(points, min_threshold=0.0, max_threshold=5.0, flag=1):
    """

    :param points:
    :param min_threshold
    :param max_threshold
    :param flag: default 1: 计算y loss; 0:计算 x loss
    :return:
    """
    loss_sum = 0
    nums = 0
    for point in points:
        if flag == 1 and max_threshold >= point[3] >= min_threshold:
            print(point)
            loss = abs(point[1] - point[3])
            loss_sum += loss
            nums += 1
        elif flag == 0 and min_threshold <= abs(point[2]) <= max_threshold:
            print(point)
            loss = abs(point[0] - point[2])
            loss_sum += loss
            nums += 1
    if nums == 0:
        return
    average_loss = loss_sum / nums
    return average_loss

def collect_folder(root_path):
    file_names = os.listdir(root_path)
    collect_data = []
    for file in file_names:
        if "txt" not in file:
            continue
        filename = os.path.join(root_path, file)
        part_data = collect_fuction(filename)
        collect_data.extend(part_data)
    return collect_data


def horizontal_loss(input_data, index_horizon, flag=0):
    """

    :param input_data: 输入
    :param index_horizon: 行号
    :param flag: default 0: 计算x loss, 1:计算y loss
    :return:
    """
    array_data = extract_x_y_data(input_data, 0, index_horizon, 1)
    loss = 0
    if flag == 0:
        loss = calc_soft_loss(array_data, flag=0)
    elif flag == 1:
        loss = calc_soft_loss(array_data, flag=1)
    return loss


def vertical_loss(input_data, index_vertical, flag=0):
    """

    :param input_data: 输入
    :param index_vertical: 列号
    :param flag: default 0: 计算x loss, 1:计算y loss
    :return:
    """
    array_data = extract_x_y_data(input_data, index_vertical, index_vertical, 0)
    loss = 0
    if flag == 0:
        loss = calc_soft_loss(array_data, flag=0)
    elif flag == 1:
        loss = calc_soft_loss(array_data, flag=1)
    return loss


def test():
    data = collect_folder("optimized_folder")
    avg_x_loss_calibration = calc_soft_loss(data, 0., 5., 0)
    avg_y_loss_calibration = calc_soft_loss(data, 0., 5., 1)
    avg_3_x_loss_calibration = calc_soft_loss(data, 0., 3., 0)
    avg_3_y_loss_calibration = calc_soft_loss(data, 0., 3., 1)
    print(avg_x_loss_calibration)
    print(avg_y_loss_calibration)
    print(avg_3_x_loss_calibration)
    print(avg_3_y_loss_calibration)
    print("data nums: ", len(data))


if __name__ == '__main__':
    test()
    # # stringName = "data.txt"
    # stringName = "blackShoesUpResult.txt"
    # white = "whiteShoesUpResult.txt"
    # calibration = "calib200Up.txt"
    # all_data = collect_fuction(stringName)
    # white_data = collect_fuction(white)
    # print(all_data)
    # print(white_data)
    # all_data.extend(white_data)
    # print(all_data)
    # calibration_data = collect_fuction(calibration)
    # extract_data = extract_x_y_data(all_data, 0, 1, 0)
    # extract_white = extract_x_y_data(white_data, 0, 1, 0)
    # extract_calibration_data = extract_x_y_data(calibration_data, 0, 2, 1)
    #
    # # avg_loss = calc_loss(extract_data, 0, 1, 0)
    # avg_loss = calc_soft_loss(extract_data, 4.)
    # avg_loss1 = calc_soft_loss(extract_white, 4.)
    # avg_loss_calibration = calc_soft_loss(extract_calibration_data, 0., 5., 0)
    # print(avg_loss)
    # print(avg_loss1)
    # print(avg_loss_calibration)
